class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        int n = nums.size(), sum = 0;
        for (int num : nums)
            sum += num;

        if (sum < target || (target & 1) ^ (sum & 1) == 1)
            return 0;

        int tmp = (target + sum) >> 1;
        if (tmp < 0)
            return 0;
        vector<int> dp(tmp + 1);
        dp[0] = 1;
        for (int e : nums)
        {
            for (int j = tmp; j >= e; j--)
            {
                dp[j] += dp[j - e];
            }
        }
        return dp[tmp];
    }
};